package com.itany.corejava.code08_集合.collections;

/**
 * @author MissChen
 * @date 2024年03月06日 11:25
 */

import java.util.*;

/**
 * 创建Stu类，有name、score属性
 * 分别提示用户输入五个学生姓名、分数，存入到ArrayList中
 * 最后按照分数降序展示在控制台
 */
public class Test03_练习 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<Stu> list=new ArrayList<>();
        for(int i=0;i<5;i++){
            System.out.print("请输入学生姓名:");
            String name=sc.next();
            System.out.print("请输入学生分数:");
            double score=sc.nextDouble();
            Stu stu=new Stu(name,score);
            list.add(stu);
        }
        System.out.println(list);
        Collections.sort(list, new Comparator<Stu>() {
            @Override
            public int compare(Stu o1, Stu o2) {
                if(o1.getScore()>o2.getScore()){
                    return -1;
                }else if(o1.getScore()<o2.getScore()){
                    return 1;
                }
                return 0;
            }
        });
        System.out.println(list);




    }
}
class Stu{
    private String name;
    private double score;

    public Stu(String name, double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}